package com.mbc.rmt.manager;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.mbc.common.exception.DataDuplicationException;
import com.mbc.common.manager.AbstractMgr;
import com.mbc.common.param.CriteriaParams;
import com.mbc.common.utils.KeyGenerator;
import com.mbc.rmt.dao.CashPaymentBySentDAO;
import com.mbc.rmt.dao.CashPaymentBySentDetailDAO;
import com.mbc.rmt.dao.RemittanceDAOFactory;
import com.mbc.rmt.data.CashPaymentBySentDataList;
import com.mbc.rmt.entities.CashPaymentBySent;
import com.mbc.rmt.entities.CashPaymentBySentDetail;

public class CashPaymentBySentMgr extends AbstractMgr{
	
	private CashPaymentBySentDAO cashPaymentBySentDAO;
	
	public CashPaymentBySentMgr(){}
	
	public CashPaymentBySentMgr(String sessionId){
		this.sessionId =  sessionId;
	}
	public void create(CashPaymentBySent entity) throws Exception {
		openConnection();
		try {
			beginTransaction();
			cashPaymentBySentDAO = RemittanceDAOFactory.getCashPaymentBySentDAO(getConnection());
			entity.setCashPaymentBySent_id(KeyGenerator.generateKey(getConnection()));
			cashPaymentBySentDAO.create(entity);

			CashPaymentBySentDetailDAO l_CashPaymentBySentDetail = RemittanceDAOFactory
					.getCashPaymentBySentDetailDAO(getConnection());
			for (CashPaymentBySentDetail l_detail : entity.getCashPaymentBySentDetailList()) {
				l_detail.setSlip_id(entity.getCashPaymentBySent_id());
				l_detail.setCashPaymentBySentDetail_id(KeyGenerator
						.generateKey(getConnection()));
				l_CashPaymentBySentDetail.create(l_detail);
			}
			commintTransaction();
		} catch (SQLException e) {
			rollbackTransaction();
			if(DataDuplicationException.UNIQUE_KEY_ERROR_CODE == e.getErrorCode())
				throw new DataDuplicationException(e.getMessage());
			else
				throw e;
		} finally {
			closeConnection();
		}
	}
	public void createDetail(CashPaymentBySentDetail entity) throws Exception {
		openConnection();
		try {
			CashPaymentBySentDetailDAO l_CashPaymentBySentDetail = RemittanceDAOFactory
					.getCashPaymentBySentDetailDAO(getConnection());
			entity.setCashPaymentBySentDetail_id(KeyGenerator.generateKey(getConnection()));
			l_CashPaymentBySentDetail.create(entity);
		} catch (Exception e) {
			throw e;
		} finally {
			closeConnection();
		}
	}
	public void update(CashPaymentBySent entity) throws Exception {
		openConnection();
		try {
			beginTransaction();
			cashPaymentBySentDAO = RemittanceDAOFactory.getCashPaymentBySentDAO(getConnection());
			cashPaymentBySentDAO.update(entity);
			CashPaymentBySentDetailDAO l_CashPaymentBySentDetailDAO = RemittanceDAOFactory
					.getCashPaymentBySentDetailDAO(getConnection());
			l_CashPaymentBySentDetailDAO.deleteByCashPaymentBySentID(entity.getCashPaymentBySent_id());
			for (CashPaymentBySentDetail l_detail : entity.getCashPaymentBySentDetailList()) {
				l_detail.setSlip_id(entity.getCashPaymentBySent_id());
				l_detail.setCashPaymentBySentDetail_id(KeyGenerator
						.generateKey(getConnection()));
				l_CashPaymentBySentDetailDAO.create(l_detail);
			}
			commintTransaction();
		} catch (Exception e) {
			rollbackTransaction();
			throw e;
		} finally {
			closeConnection();
		}
	}
	public void delete(long key) throws Exception {
		openConnection();
		try {
			beginTransaction();
			cashPaymentBySentDAO = RemittanceDAOFactory.getCashPaymentBySentDAO(getConnection());
			CashPaymentBySentDetailDAO l_CashPaymentBySentDetailDAO = RemittanceDAOFactory.getCashPaymentBySentDetailDAO(getConnection());
			l_CashPaymentBySentDetailDAO.deleteByCashPaymentBySentID(key);
			cashPaymentBySentDAO.delete(key);
			commintTransaction();
		} catch (Exception e) {
			rollbackTransaction();
			throw e;
		} finally {
			closeConnection();
		}
	}
	public CashPaymentBySent find(long key) throws Exception {
		openConnection();
		try {
			cashPaymentBySentDAO = RemittanceDAOFactory.getCashPaymentBySentDAO(getConnection());
			CashPaymentBySent l_CashPaymentBySent = cashPaymentBySentDAO.find(key);
			CashPaymentBySentDetailDAO l_CashPaymentBySentDetailDAO = RemittanceDAOFactory.getCashPaymentBySentDetailDAO(getConnection());
			l_CashPaymentBySent.setCashPaymentBySentDetailList(l_CashPaymentBySentDetailDAO.findByCashPaymentBySentID(key));
			return l_CashPaymentBySent;
		} catch (Exception e) {
			throw e;
		} finally {
			closeConnection();
		}
	}
	public List<CashPaymentBySent> findAll() throws Exception {
		openConnection();
		try {
			cashPaymentBySentDAO = RemittanceDAOFactory.getCashPaymentBySentDAO(getConnection());
			return cashPaymentBySentDAO.findAll();
		} catch (Exception e) {
			throw e;
		} finally {
			closeConnection();
		}
	}
	public CashPaymentBySentDataList findCashPaymentBySents(CriteriaParams cri)throws Exception{
		openConnection();
		try {
			cashPaymentBySentDAO = RemittanceDAOFactory.getCashPaymentBySentDAO(getConnection());
			return cashPaymentBySentDAO.findCashPaymentBySents(cri);
		} catch (Exception e) {
			throw e;
		} finally {
			closeConnection();
		}
	}
	public ArrayList<CashPaymentBySentDetail> findCashPaymentBySentDetails(long cashPaymentBySentId)
			throws Exception {
		openConnection();
		try {
			ArrayList<CashPaymentBySentDetail> l_list = new ArrayList<CashPaymentBySentDetail>();
			cashPaymentBySentDAO = RemittanceDAOFactory.getCashPaymentBySentDAO(getConnection());
			l_list = cashPaymentBySentDAO.findCashPaymentBySentDetails(cashPaymentBySentId);
			return l_list;
		} catch (Exception e) {
			throw e;
		} finally {
			closeConnection();
		}
	}

	public CashPaymentBySent getCashPaymentBySent(CashPaymentBySent l_info) {
		// TODO Auto-generated method stub
		return null;
	}
}
